<?php
if ($ticket_parent!=1) {
	header('Location: class_manager.php');
	exit;
}

if(isset($_POST["add_module_form_nr_prereqs"]) && intval($_POST["add_module_form_nr_prereqs"])>=0 && intval($_POST["add_module_form_nr_prereqs"])<=5){
	$nr_prereqs = intval($_POST["add_module_form_nr_prereqs"]);
}
else{
	$nr_prereqs = 0;
}

?>

<script type="text/javascript">
	function expand_collapse_div(element_id){
		var t=document.getElementById(element_id);
		if(!t)return false;
		if(t.style.display=="none"){
			t.style.display="block";
		}
		else{
			t.style.display="none";
		}
		return true;
	}		
</script>


<div class="form">
<h2 class="spaced">Add Module</h2>
<form name="add_module_form"
	action="class_manager.php?page=admin&admin_page=manage_modules"
	method="post">
<table class="form">
	<tr>
		<td align="right">Long Name:</td>
		<td align="left"><input type="text" style="width: 400px"
			name="add_module_form_long_name" <?php if(isset($_POST["add_module_form_long_name"])) echo 'value="'.htmlspecialchars($_POST["add_module_form_long_name"]).'"';?>></td>
	</tr>
	<tr>
		<td align="right">Short Name:</td>
		<td align="left"><input type="text" style="width: 400px"
			name="add_module_form_short_name" <?php if(isset($_POST["add_module_form_short_name"])) echo 'value="'.htmlspecialchars($_POST["add_module_form_short_name"]).'"';?>></td>
	</tr>
	<tr>
		<td valign="top" align="right">Description:</td>
		<td align="left"><textarea rows="10" style="width: 400px"
			name="add_module_form_description"><?php if(isset($_POST["add_module_form_description"])) echo htmlspecialchars($_POST["add_module_form_description"]);?></textarea></td>
	</tr>
	
	
	
	
	
	
	
	
	
	<tr>
		<td align="right">Number of Prerquisites:</td>
		<td align="left">
			<select style="width:40px;margin:0" name="add_module_form_nr_prereqs"  onchange="document.add_module_form.submit();">
				<option value="0" <?php if($nr_prereqs == 0) echo("selected");?>>0</option>
				<option value="1" <?php if($nr_prereqs == 1) echo("selected");?>>1</option>
				<option value="2" <?php if($nr_prereqs == 2) echo("selected");?>>2</option>
				<option value="3" <?php if($nr_prereqs == 3) echo("selected");?>>3</option>
				<option value="4" <?php if($nr_prereqs == 4) echo("selected");?>>4</option>
				<option value="5" <?php if($nr_prereqs == 5) echo("selected");?>>5</option>
			</select>
		</td>
	</tr>	
	<tr>
		<td colspan="2"><hr></td>
	</tr>
<?php 

for ($i=0;$i<$nr_prereqs;$i++){
	

?>

	<tr>
		<td align="right">Prerequisite <?php echo ($i+1);?>: </td>
		<td align="left">
			<select style="width:450px;margin:0" name="add_module_form_prerequisite_<?php echo $i;?>" >
				<?php
				$result = mysql_query("SELECT pkey, long_name, short_name FROM modules ORDER BY long_name ASC", $class_manager_db) or die(mysql_error($class_manager_db));
				while ($line = mysql_fetch_array($result,MYSQL_ASSOC)) {
					echo '<option value="'. $line["pkey"] .'"';
					if(isset($_POST["add_module_form_prerequisite_".$i]) && $_POST["add_module_form_prerequisite_".$i]==$line["pkey"])
						echo 'selected';
					echo '>'. htmlspecialchars(stripslashes($line["long_name"])) .' ('.htmlspecialchars(stripslashes($line["short_name"])).')</option>';
				}
				?>
			</select>
		</td>
	</tr>
<?php 
}
?>	

	<tr>
		<td colspan="2"><hr></td>
	</tr>
	<tr>
		<td align="left" colspan="2">
		<b>Important!</b> This tool does <b>not</b> check for circular prerequisite dependencies!  Please make sure this does not happen as no one will be able to register for this class if this is the case!
		</td>
	</tr>
	
	
	
	
	
	
	
	
	
	<tr>
		<td colspan="2"><input type="submit" name="add_module_form_submit" value="Add New Module"/>
		</td>
	</tr>
</table>
</form>
</div>
<hr>


<div class="present">
<h2 class="spaced">Exisiting Modules</h2>
Click a module name to expand it for editing.
<table>
<?php 
$i = 0;
$result = mysql_query("SELECT * FROM modules ORDER BY short_name ASC", $class_manager_db) or die(mysql_error($class_manager_db));
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
	<tr><td><hr></td></tr>
	<tr><td class="present">
		<a onclick="expand_collapse_div('module_details_<?php echo $i;?>')" class="clickable">
			<?php echo htmlspecialchars(stripslashes($line["long_name"]))." (".htmlspecialchars(stripslashes($line["short_name"])).")"?>
		</a>
	</td></tr>
	<tr><td>
		<div id="module_details_<?php echo $i;?>" style="display:none;">
		
		
		
		
		<form name="update_module_form"
				action="class_manager.php?page=admin&admin_page=manage_modules"
				method="post">
			<table class="form">
				<tr>
					<td align="right" style="width:100px;">Long Name:</td>
					<td align="left"><input type="text" style="width: 400px"
						name="update_module_form_long_name" value="<?php echo htmlspecialchars(stripslashes($line["long_name"]));?>"></td>
				</tr>
				<tr>
					<td valign="top" align="right">Description:</td>
					<td align="left"><textarea rows="10" style="width: 400px"
						name="update_module_form_description"><?php echo htmlspecialchars(stripslashes($line["description"]));?></textarea></td>
				</tr>
				<tr>
					<td colspan="2">
						<input type="hidden" name="update_module_form_pkey" value="<?php echo $line["pkey"];?>">
						<input type="submit" name="update_module_form_submit" value="Update Module"/>
					</td>
				</tr>
			</table>
			</form>
			<table class="form">
<?php 
	$j=0;
	$prereq_result = mysql_query("SELECT * FROM prerequisites LEFT JOIN modules ON prerequisites.prerequisite=modules.pkey WHERE module='".$line["pkey"]."'", $class_manager_db) or die(mysql_error($class_manager_db));
	while ($prereq_line = mysql_fetch_array($prereq_result, MYSQL_ASSOC)) {
?>
				<tr>
					<td align="right" style="width:100px;">Prerequisite <?php echo ($j+1);?>:</td>
					<td align="left"><?php echo htmlspecialchars($prereq_line["long_name"])." (".htmlspecialchars($prereq_line["short_name"]).")";?></td>
					<td align="right" style="width:100px;">
						<form name="delete_prerequisite_form" 
							action="class_manager.php?page=admin&admin_page=manage_modules"
							method="post">
							<input type="hidden" name="delete_prerequisite_form_module" value="<?php echo $prereq_line["module"];?>">
							<input type="hidden" name="delete_prerequisite_form_prerequisite" value="<?php echo $prereq_line["prerequisite"];?>">
							<input type="submit" name="delete_prerequisite_form_submit" value="Remove Prerequisite"/>
						</form>
					</td>
				</tr>

<?php 
		$j++;
	}
?>
			</table>
			
			
			
			<form name="add_prerequisite_form" 
				action="class_manager.php?page=admin&admin_page=manage_modules"
				method="post">
			<table class="form">
				<tr>
					<td align="right" style="width:100px;">New Prerequisite: </td>
					<td align="left">
						<select style="width:300px;margin:0" name="add_prerequisite_form_prerequisite" >
							<?php
							$prereq_result = mysql_query("SELECT pkey, long_name, short_name FROM modules ORDER BY long_name ASC", $class_manager_db) or die(mysql_error($class_manager_db));
							while ($prereq_line = mysql_fetch_array($prereq_result,MYSQL_ASSOC)) {
								echo '<option value="'. $prereq_line["pkey"] .'">'. htmlspecialchars(stripslashes($prereq_line["long_name"])) .' ('.htmlspecialchars(stripslashes($prereq_line["short_name"])).')</option>';
							}
							?>
						</select>
					</td>
					<td style="width:100px;">
						<input type="hidden" name="add_prerequisite_form_module" value="<?php echo $line["pkey"];?>">
						<input type="submit" name="add_prerequisite_form_submit" value="Add Prerequisite"/>
					</td>
				</tr>
				<tr>
					<td align="left" colspan="3">
					<b>Important!</b> This tool does <b>not</b> check for circular prerequisite dependencies!  Please make sure this does not happen as no one will be able to register for this class if this is the case!
					</td>
				</tr>
			</table>
			</form>
		
		
		
		</div>
	</td></tr>
<?php
$i++;
}
?>
	<tr><td><hr></td></tr>
</table>
</div>



